package com.ysd.lis.service.inv.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.inv.*;
import com.ysd.lis.mapper.inv.InvReagentMapper;
import com.ysd.lis.request.inv.InvReagentDto;
import com.ysd.lis.request.inv.InvReagentParam;
import com.ysd.lis.service.inv.InvReagentService;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-09-12
 */
@Service
public class InvReagentServiceImpl extends ServiceImpl<InvReagentMapper, InvReagent> implements InvReagentService {
    @Autowired
    InvReagentMapper invReagentMapper;

    @Override
    public Result findInvReagentList(InvReagentParam param) {

        MPJLambdaWrapper<InvReagent> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(InvReagent.class)
                .selectAs("ins", InvSupplier::getSupplierName, InvReagent::getVendorName)
                .selectAs("ma", InvManufacturer::getManuName, InvReagent::getManuName)
                .selectAs("ca", InvReagentCategorize::getCategorizeName, InvReagent::getCategorizeName)
                .selectAs("ce", InvCert::getCertName, InvReagent::getCertName)
                .leftJoin(InvSupplier.class, "ins",p->p.eq(InvSupplier::getSupplierCode, InvReagent::getVendorNo).eq(InvSupplier::getDelFlag, 0))
                .leftJoin(InvManufacturer.class, "ma",p->p.eq(InvManufacturer::getManuCode, InvReagent::getManufacturerNo).eq(InvManufacturer::getDelFlag, 0))
                .leftJoin(InvReagentCategorize.class, "ca",p->p.eq(InvReagentCategorize::getId, InvReagent::getCategorizeId).eq(InvReagent::getDelFlag, 0) )
                .leftJoin(InvCert.class, "ce", p->p.eq(InvCert::getCertCode, InvReagent::getCertNo).eq(InvCert::getDelFlag, 0) )
                .eq(InvReagent::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getSearchValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvReagent::getShortName, param.getSearchValue())
                    .or().like(InvReagent::getReaNo, param.getSearchValue())
                    .or().like(InvReagent::getBarcode, param.getSearchValue())
                    .or().like(InvReagent::getReaNa, param.getSearchValue()));
        }
        if(ToolsUtils.isNotEmpty(param.getIsBatchNumMgt())){
            lambdaQueryWrapper.eq(InvReagent::getIsBatchNumMgt,param.getIsBatchNumMgt());
        }
        if(ToolsUtils.isNotEmpty(param.getIsSingleBarcode())){
            lambdaQueryWrapper.eq(InvReagent::getIsSingleBarcode,param.getIsSingleBarcode());
        }
        if(ToolsUtils.isNotEmpty(param.getVendorNo())){
            lambdaQueryWrapper.eq(InvReagent::getVendorNo,param.getVendorNo());
        }
        if(ToolsUtils.isNotEmpty(param.getIsSingle())&&param.getIsSingle()){
            lambdaQueryWrapper.eq(InvReagent::getIsSingle,param.getIsSingle());
        }
        if (StringUtils.isNotBlank(param.getCategorizeId())) {
            lambdaQueryWrapper.eq(InvReagent::getCategorizeId, param.getCategorizeId());
        }

        lambdaQueryWrapper.orderByAsc(InvReagent::getSeq);
        List<InvReagent> materialList = invReagentMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", materialList);
    }

    @Override
    public Result addInvReagent(InvReagent invReagent) {
        int insert = invReagentMapper.insert(invReagent);
        if (insert > 0) {
            return Result.succ(1, "添加成功", invReagent);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result editInvReagent(InvReagent invReagent) {
        int updateById = invReagentMapper.updateById(invReagent);
        if (updateById > 0) {
            return Result.succ(1, "提交成功", invReagent);
        } else {
            return Result.fail(400, "修改失败", "");
        }
    }

    @Override
    public Result batchDeleteInvReagentList(InvReagentDto dto) {
        List<InvReagent> list = dto.getInvReagentList();
        List<String> ids = list.stream().map(InvReagent::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
